*--------------------------------------------------------------; * Selects a single-stage cluster sample, in which clusters ; * are selected (with replacement) with probability propor- ; * tional to size of cluster. ; *--------------------------------------------------------------; %macro cl1pps(noprint,frame=,cluster=,sample=,npop=,n=, setup=,seed=,rep=); %if %length(&seed) = 0 %then %let seed = 0; %if %length(&frame) = 0 %then %let frame = %str(frame); %if %length(&sample) = 0 %then %let sample = %str(sample); %if %length(&cluster) = 0 %then %let cluster = %str(cluster); %if %length(&n) = 0 %then %let n = %str(n); %if %length(&rep) = 0 %then %let rep = %str(rep); proc sort data = &frame; by &cluster; proc means data = &frame noprint; by &cluster; output out = weights_ n = mi_; data weights_; set weights_ end = eof; cum_ + mi_; if eof = 1 then do; call symput('tot_',trim(left(cum_))); end; drop _type_ _freq_; run; data weights_; set weights_; pi_ = mi_/&tot_; u_ = cum_/&tot_; data unifs_(keep = u_); %if %length(&setup) > 0 %then %do; set &setup (keep = &n); %end; do i_ = 1 to &n; u_ = ranuni(&seed); output; end; proc sort data = unifs_; by u_; data outcl_; set weights_ unifs_; by u_; data outcl_; set outcl_; retain count_ 0; flag_ = 1; if cum_ = . then count_ = count_ + 1; else do; if count_ > 0 then do; do i_ = 1 to count_; output; end; count_ = 0; end; end; drop cum_ u_ i_; proc sort data = outcl_; by &cluster; data &sample(drop = flag_ mi_ pi_ i_ count_); merge outcl_ &frame; by &cluster; if flag_ = 1 then do; do i_ = 1 to count_; &rep = i_; output; end; end; proc sort data = &sample; by &cluster &rep; %if %length(&noprint) = 0 %then %do; proc print data = outcl_ noobs; title1 'Clusters Selected at First Stage'; title2 '(According to PPS Design)'; var &cluster; proc print data = &sample; title1 'Single-stage Cluster Sample'; title2 'Clusters Selected with Probability Proportional to Size'; title3 '(Replicates, if any, are reported as multiple entries)'; title4 "Output Data Set = &sample"; %end; run; title; %mend cl1pps;